package com.tz.farmer.controller;

import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.service.IFarmerSunDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
@RequestMapping("/summary")
@Api(value = "链条追溯汇总")
public class SummaryController {

    @Autowired
    private IFarmerSunDataService dataService;

    private static final String bodyKey = "bodyKey";
    private static final String productKey = "productKey";
    private static final String chainKey = "chainKey";
    private static final String chainProductKey = "chainProductKey";
    private static final String percentKey = "percentKey";
    private static final String sendCheckKey = "sendCheckKey";
    private static final String market = "大屏汇总";

    @GetMapping("/get")
    @ApiOperation(value = "获取数据")
    public ResultEx getConfig() {
        Map<String, Object> response = new TreeMap<>();

        response.put("body", dataService.getData(market, bodyKey));
        response.put("product", dataService.getData(market, productKey));
        response.put("chain", dataService.getData(market, chainKey));
        response.put("chainProduct", dataService.getData(market, chainProductKey));
        response.put("percent", dataService.getData(market, percentKey));
        response.put("sendCheck", dataService.getData(market, sendCheckKey));

        return ResultEx.builder().makeSuccessResult(response);
    }

    @PostMapping("/set-body")
    @ApiOperation(value = "设置激活主体数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setBodyCount(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, bodyKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-product")
    @ApiOperation(value = "设置激活产品数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setProductCount(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, productKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-chain-body")
    @ApiOperation(value = "设置上链主体数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setChainBodyCount(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, chainKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-chain-product")
    @ApiOperation(value = "设置上链产品数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setChainProductCount(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, chainProductKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-ok-percent")
    @ApiOperation(value = "设置食品安全满意度")
    @ApiImplicitParam(name = "percent", value = "百分比(不带百分号)", dataType = "Decimal")
    public ResultEx setPercentCount(BigDecimal percent) {
        if (ObjectUtils.isEmpty(percent) || percent.doubleValue() <= 0.00) {
            return ResultEx.builder().makeFailedResult("参数[percent]值错误");
        }

        if (dataService.setData(market, percentKey, percent.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-send-check")
    @ApiOperation(value = "设置转检案数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Decimal")
    public ResultEx setSenCheckCount(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0.00) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, sendCheckKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }
}
